var express = require('express');
var router = express.Router();

var conn = require('../conn');

/*-----文件上传------*/
var upload_dir = "public/uploads/"
var multer  = require('multer')
var upload = multer({ dest: upload_dir })
var fs = require("fs")

//中间件
/*router.use(function (req, res, next) {
 	if (req.session.if_login == true) {
		next();
 	}else{
 		res.redirect('adminusers/login');
 	}
});*/

/* GET adminusers listing. */
router.get('/', function(req, res, next) {
	conn.query("select count(*) as c from product",function(err,rows,fields){
		req.product_num = rows[0].c;
		next();
	});
},function(req, res, next){
	if (req.query.page == undefined) {
		var page=1
	}else{
 		page = req.query.page;
	}
	var pageSize = 6;
	var pageLength = Math.ceil(req.product_num/pageSize);
	conn.query("select *,product.id as aid from product join catalog on product.cata_id=catalog.id order by aid DESC limit "+(page-1)*pageSize+","+pageSize,function(err,rows,fields){
		res.render('productList', {item:rows,username:req.session.username,success:req.flash('success'),page:pageLength,page_id:page-1,article_num:req.article_num});		
	});
});

router.get('/addProduct', function(req, res, next) {
	var sql = "select * from catalog";
	conn.query(sql,function(err,rows,fields){
		var cata = rows;
		conn.query("select * from producttype",function(err,rows,fields){
			res.render('addProduct', { item:rows ,cata:cata} );
		});
	});
});
router.post('/addProduct', function(req, res, next) {
	if (req.body.type_id=='$') {
		res.send("分类未选择");
	}else if (req.body.cata_id=='$'){
		res.send("分类2未选择");
	}else if (req.body.price==''){
		res.send("请输入价格");
	}else if (req.body.images==''){
		res.send("未上传图片");
	}else if (req.body.content==''){
		res.send("内容不能为空");
	}
	else{
		conn.query("insert into product value(null,'"+req.body.type_id+"','"+req.body.cata_id+"','"+req.body.price+"','"+req.body.content+"','"+req.body.images+"')",function(err,rows,fields){
			req.flash('success', '添加成功!')
			res.send("ok");
		});
	}
});

router.get('/editProduct', function(req, res, next) {
	var sql = "select * from catalog";
	conn.query(sql,function(err,rows,fields){
		var cata = rows;
		conn.query("select * from producttype",function(err,rows,fields){
			var type = rows;
			conn.query("select * from product where id="+req.query.id,function(err,rows,fields){
				res.render('editProduct', { item:rows ,cata:cata,type:type,product_id:req.query.id} );
			});
		})
	});
});
router.post('/editProduct', function(req, res, next) {
	if (req.body.type_id=='$') {
		res.send("分类未选择");
	}else if (req.body.cata_id=='$'){
		res.send("分类2未选择");
	}else if (req.body.price==''){
		res.send("请输入价格");
	}else if (req.body.images==''){
		res.send("未上传图片");
	}else if (req.body.content==''){
		res.send("内容不能为空");
	}else{
		conn.query("update huali.product set type_id='"+req.body.type_id+"',cata_id='"+req.body.cata_id+"',price='"+req.body.price+"',abstract='"+req.body.content+"',img='"+req.body.images+"' where id="+req.body.product_id,function(err,rows,fields){
			req.flash('success', '更新成功!')
			res.send("ok");
		});
	}
});

router.get('/delProduct', function(req, res, next) {
	conn.query("delete from product where id="+req.query.id,function(err,rows,fields){
		res.redirect('/admin');
	});
});
router.post('/upload',upload.single('fileInput'), function(req, res, next) {
	fs.renameSync(upload_dir+req.file.filename,upload_dir+req.file.originalname);
	var img_dir = "uploads/"+req.file.originalname;
	res.send(img_dir)
});
router.get('/signout', function(req, res, next) {
	req.session.destroy(function(err) {
		res.redirect('/adminusers/login');
	})
});
module.exports = router;